﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

/// <summary>
/// Summary description for ListHeader
/// </summary>
public class ListHeader : System.Web.UI.UserControl
{
    protected HtmlTableCell tdHeader;
    protected HtmlTableCell tdHeaderButton;
    protected Button btnSort;
    protected Image imgTitle;
    protected Image imgTitleButton;
    protected Literal ltTitle;
    protected Literal ltTitleButton;
    protected Literal ltDirection;
    protected CheckBox cbCheckAll;

    public ListHeaderTypeCode Type
    {
        get
        {
            if (ViewState["Type"] == null)
            {
                ViewState["Type"] = ListHeaderTypeCode.Column;
            }
            return (ListHeaderTypeCode)ViewState["Type"];
        }
        set
        {
            ViewState["Type"] = value;
        }
    }

    public string Title
    {
        get
        {
            if (ViewState["Title"] == null)
            {
                ViewState["Title"] = string.Empty;
            }
            return ViewState["Title"].ToString();
        }
        set
        {
            ViewState["Title"] = value;
        }
    }

    public string ImageSrc
    {
        get
        {
            if (ViewState["ImageSrc"] == null)
            {
                ViewState["ImageSrc"] = string.Empty;
            }
            return ViewState["ImageSrc"].ToString();
        }
        set
        {
            ViewState["ImageSrc"] = value;
        }
    }

    public string Width
    {
        get
        {
            if (ViewState["Width"] == null)
            {
                ViewState["Width"] = string.Empty;
            }
            return ViewState["Width"].ToString();
        }
        set
        {
            ViewState["Width"] = value;
        }
    }

    public string OrderColumn
    {
        get
        {
            if (ViewState["OrderColumn"] == null)
            {
                ViewState["OrderColumn"] = string.Empty;
            }
            return ViewState["OrderColumn"].ToString();
        }
        set
        {
            ViewState["OrderColumn"] = value;
        }
    }

    public string FirstOrderDirection
    {
        get
        {
            if (ViewState["FirstOrderDirection"] == null)
            {
                ViewState["FirstOrderDirection"] = string.Empty;
            }
            return ViewState["FirstOrderDirection"].ToString();
        }
        set
        {
            ViewState["FirstOrderDirection"] = value;
        }
    }

    public string Align
    {
        get
        {
            if (ViewState["Align"] == null)
            {
                ViewState["Align"] = "left";
            }
            return ViewState["Align"].ToString();
        }
        set
        {
            ViewState["Align"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        tdHeaderButton.Attributes.Add("onclick", string.Format("document.getElementById('{0}').click()", btnSort.ClientID));

        tdHeader.Width = Width;
        tdHeaderButton.Width = Width;

        tdHeader.Align = Align;
        tdHeaderButton.Align = Align;


        if (Type == ListHeaderTypeCode.CheckBox)
        {
            OrderColumn = string.Empty;
            imgTitle.Visible = false;
            cbCheckAll.Attributes.Add("onclick", "selectAll(this);");
            
        }
        else
        {
            cbCheckAll.Visible = false;

            if (ImageSrc != string.Empty)
            {
                imgTitle.ImageUrl = ImageSrc;
                imgTitle.AlternateText = Title;

                imgTitleButton.ImageUrl = ImageSrc;
                imgTitleButton.AlternateText = Title;
            }
            else
            {
                imgTitle.Visible = false;
                ltTitle.Text = Title;

                imgTitleButton.Visible = false;
                ltTitleButton.Text = Title;
            }
        }

        if (this.OrderColumn != string.Empty)
        {
            tdHeaderButton.Visible = true;
            tdHeader.Visible = false;
        }
        else
        {
            tdHeaderButton.Visible = false;
            tdHeader.Visible = true;
        }
    }


    protected void btnSort_Click(object sender, EventArgs e)
    {
        Order currentOrder = (this.Page.Master as ListMaster).Order;
        if (currentOrder != null)
        {
            if (currentOrder.OrderColumn == this.OrderColumn)
            {
                if (currentOrder.OrderDirection == "DESC")
                    currentOrder.OrderDirection = string.Empty;
                else
                    currentOrder.OrderDirection = "DESC";
            }
            else
            {
                currentOrder.OrderColumn = this.OrderColumn;
                currentOrder.OrderDirection = this.FirstOrderDirection;
            }
        }
    }

    protected override void OnPreRender(EventArgs e)
    {
        if (this.OrderColumn != string.Empty)
        {
            Order currentOrder = (this.Page.Master as ListMaster).Order;
            if (this.OrderColumn == currentOrder.OrderColumn)
            {
                this.tdHeaderButton.Attributes.Add("class", "Cur");
                switch (currentOrder.OrderDirection)
                {
                    case "DESC":
                        ltDirection.Text = "↓";
                        break;
                    case "":
                        ltDirection.Text = "↑";
                        break;
                }
            }
            else
            {
                this.tdHeaderButton.Attributes.Remove("class");
                ltDirection.Text = string.Empty;
            }
        }

    }
}
